/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.latestbit.smartobjects.network.tests;

import static org.junit.Assert.*;

import java.net.URI;
import java.util.concurrent.TimeUnit;

import org.junit.Test;
import org.latestbit.smartobjects.ISmartBus;
import org.latestbit.smartobjects.SmartBus;
import org.latestbit.smartobjects.network.*;
import org.latestbit.smartobjects.network.tcp.tests.TCPTestClient;
import org.latestbit.smartobjects.network.tcp.tests.TCPTestServer;

public class ManagedNetworkTest {

	@Test
	public void testCreateManagedClient() throws Exception {
		ISmartBus bus = new SmartBus();
		ManagedNetwork network = new ManagedNetwork();
		IManagedClient client = network.createClient();
		
		URI serverURI = new URI("tcp://localhost:3333");
		TCPTestServer server = new TCPTestServer(serverURI);
		
		server.getAcceptor().start();

		client.setEndpointLocation( new EndpointLocation(bus, serverURI), bus.getCoordinatorFactory().createDefaultCoordinator());
		ManagedConnectionListenerTester mcListener = new ManagedConnectionListenerTester();
		client.addConnectionListener(mcListener);
				
		mcListener.getConditionLock().lock();
		client.start();
		mcListener.waitCondition();
		mcListener.getConditionLock().unlock();
		
		assertTrue ( mcListener.isConnected() );
		
		client.delConnectionListener(mcListener);
		client.stop();
		
		mcListener = new ManagedConnectionListenerTester();
		client.addConnectionListener(mcListener);

		client.start(25, TimeUnit.SECONDS);
		
		assertTrue ( client.isOpen() );
		
		client.stop();
		
		assertFalse ( client.isOpen() );
		
		server.getAcceptor().stop();
	}
	
	@Test
	public void testCreateFailOverEnvironment() throws Exception {
		ISmartBus bus = new SmartBus();
		ManagedNetwork network = new ManagedNetwork();
		IManagedClient client = network.createClient();
		
		URI serverURI1 = new URI("tcp://localhost:3333");
		TCPTestServer server1 = new TCPTestServer(serverURI1);

		URI serverURI2 = new URI("tcp://localhost:3334");
		TCPTestServer server2 = new TCPTestServer(serverURI2);
		
		server1.getAcceptor().start();
		server2.getAcceptor().start();
		
		EndpointLocation reference = new EndpointLocation(bus);
		reference.getLocations().add(serverURI1);
		reference.getLocations().add(serverURI2);
		client.setEndpointLocation(reference, bus.getCoordinatorFactory().createDefaultCoordinator());
		
		ManagedConnectionListenerTester mcListener = new ManagedConnectionListenerTester();
		client.addConnectionListener(mcListener);
				
		mcListener.getConditionPerHandlerLock().lock();
		client.start();
		mcListener.waitConditionPerHandler();
		if(mcListener.getLastHandlers().size() == 1)
			mcListener.waitConditionPerHandler();
		mcListener.getConditionPerHandlerLock().unlock();
				
		assertTrue ( mcListener.isConnected() );
		assertTrue (mcListener.getLastHandlers().size() == 2);
		mcListener.getLastHandlers().clear();
		
		
		server1.getAcceptor().stop();
		server2.getAcceptor().stop();
	}
	
	@Test
	public void testCreateManagedServer() throws Exception {
		ISmartBus bus = new SmartBus();
		ManagedNetwork network = new ManagedNetwork();
		
		URI serverURI = new URI("tcp://localhost:3333");
		
		IManagedServer server = network.createServer();
		server.setEndpointLocation(new EndpointLocation(bus,serverURI), bus.getCoordinatorFactory().createDefaultCoordinator());
		
		ManagedServerListenerTester serverListener = new ManagedServerListenerTester();
		server.addListener(serverListener);
		
		server.start();
		
		TCPTestClient client1 = new TCPTestClient(serverURI);
		TCPTestClient client2 = new TCPTestClient(serverURI);
		
		serverListener.getConditionLock().lock();
		client1.getConnector().connect();
		client2.getConnector().connect();		
		serverListener.waitCondition();
		if(serverListener.getConnections().size() == 1) {
			serverListener.waitCondition();
		}		
		serverListener.getConditionLock().unlock();
		
		assertTrue ( serverListener.getConnections().size() == 2 );
		
		server.stop();
	}
	

}
